package com.wenhao.function;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

/**
 * @author Wenhao Tong
 * @create 2021-08-20 16:46
 */
public class StreamDemo {

    @Test
    public void test1() {
        User a = new User(1, "a", 21);
        User b = new User(1, "b", 22);
        User c = new User(1, "c", 23);
        User d = new User(1, "d", 24);
        User e = new User(1, "e", 25);
        List<User> list = Arrays.asList(a,b,c,d,e);

        list.stream().filter(u -> u.id % 2 == 1)
                .filter(u -> u.age > 22)
                .map(u -> u.name.toUpperCase())
                .sorted()
                .limit(1)
                .forEach(System.out::println);
    }

    private class User implements Comparable<User>{
        int id;
        String name;
        int age; 

        User (int id,String name,int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        @Override
        public int compareTo(StreamDemo.User o) {
            return age - o.age;
        }
    }
}
